package com.zhs.gccs.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zhs.gccs.R;
import com.zhs.gccs.model.WorkTableMenu;
import com.zhs.gccs.utils.DrawableUtils;
import com.zhs.gccs.utils.ScreenUtils;
import com.zhs.gccs.view.ShapeImageView;

import java.util.List;

public class WorktableMenuAdapter<T> extends RecyclerView.Adapter<WorktableMenuAdapter.ViewHolder> {
    private Context mContext;
    private List<WorkTableMenu> datas = null;
    private boolean editable = true;



    public void setEditable(boolean editable) {
        this.editable = editable;
        notifyDataSetChanged();
    }

    public WorktableMenuAdapter(Context context, List<WorkTableMenu> datas) {
        this.datas = datas;
        this.mContext = context;
    }
    //创建新View，被LayoutManager所调用
    @Override
    public WorktableMenuAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_view_work_menu_item,viewGroup,false);
        WorktableMenuAdapter.ViewHolder vh = new WorktableMenuAdapter.ViewHolder(view);
        return vh;
    }
    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(WorktableMenuAdapter.ViewHolder viewHolder, @SuppressLint("RecyclerView") final int position) {
        WorkTableMenu menu = datas.get(position);
        viewHolder.ll_item.setBackground(
                DrawableUtils.getShapeDrawable(ScreenUtils.dpToPx(mContext,5),
                        menu.getBackgroupColor()));
        viewHolder.tv_title.setText(menu.getTitle());
        viewHolder.tv_title_sub.setText(menu.getTitleSub());
        Glide.with(mContext).load(menu.getIcon()).into(viewHolder.v_img);
        viewHolder.ll_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(operationListener!=null)operationListener.onItemClick(position);
            }
        });
    }
    //获取数据的数量
    @Override
    public int getItemCount() {
//        if(datas!=null&&datas.size()>3)return 3;
        return datas!=null?datas.size():0;
    }
    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public View ll_item;
        public ShapeImageView v_img;
        public TextView tv_title;
        public TextView tv_title_sub;
        public ViewHolder(View view){
            super(view);
            ll_item =   view.findViewById(R.id.ll_item);
            v_img =   view.findViewById(R.id.v_img);
            tv_title =   view.findViewById(R.id.tv_title);
            tv_title_sub =   view.findViewById(R.id.tv_title_sub);
        }
    }
    public static interface OperationListener{
        void onItemClick(int position);
    }
    private WorktableMenuAdapter.OperationListener operationListener;

    public WorktableMenuAdapter.OperationListener getOperationListener() {
        return operationListener;
    }

    public void setOperationListener(WorktableMenuAdapter.OperationListener operationListener) {
        this.operationListener = operationListener;
    }
}
